package de.tabuma.sso.attribute;

import de.tabuma.sso.*;
import de.tabuma.sso.sso.*;
import de.tabuma.sso.user.*;
import de.tabuma.sso.group.*;
import de.tabuma.shopping.logik.*;
import java.util.*;

/**
 * Webservice class for AttributeFacade. All methods have parameter single sign on id.
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 * @see AttributeFacade
 * @see SSOFacade
 *
 */
public class AttributeWebservice {

    /**
     * Creating an attribute for user (given by ssoId)
     * @param attribute Attribute to be created
     * @param ssoId Single Sign On Id
     * @return id of the created attribute
     * @throws Exception
     * @see SSOFacade
     */
    public static int create(Attribute attribute, String ssoId) throws Exception {
        AttributeWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        int groupId = AttributeWebserviceLib.getLicenseGroup(ssoId);
        if(groupId!=0) {
            Group g1 = GroupFacade.get(groupId);
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.ATTRIBUTE_CREATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        return AttributeFacade.create(attribute, user.getName());
    }

    /**
     * Updating an attribute of a user (given by ssoId).
     * @param attribute Attribute to be updated
     * @param ssoId Single Sign On Id
     * @throws Exception
     */
    public static void update(Attribute attribute, String ssoId) throws Exception {
        AttributeWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        int groupId = AttributeWebserviceLib.getLicenseGroup(ssoId);
        if(groupId!=0) {
            Group g1 = GroupFacade.get(groupId);
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.ATTRIBUTE_UPDATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        AttributeFacade.update(attribute, user.getName());
    }

    /**
     * Getting an attribute of a user (given by ssoId).
     * @param id id of the attribute to be returned
     * @param ssoId Single Sign On Id
     * @return Returned Attribute
     * @throws Exception
     */
    public static Attribute get(int id, String ssoId) throws Exception {
        AttributeWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        int groupId = AttributeWebserviceLib.getLicenseGroup(ssoId);
        if(groupId!=0) {
            Group g1 = GroupFacade.get(groupId);
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.ATTRIBUTE_GET, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        return AttributeFacade.get(id);
    }

    /**
     * Removed an attribute for user (given by ssoId)
     * @param id Id of the attribute that will be removed
     * @param ssoId Single Sign On Id
     * @throws Exception
     */
    public static void remove(int id, String ssoId) throws Exception {
        AttributeWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        int groupId = AttributeWebserviceLib.getLicenseGroup(ssoId);
        if(groupId!=0) {
            Group g1 = GroupFacade.get(groupId);
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.ATTRIBUTE_REMOVE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        AttributeFacade.remove(id, user.getName());
    }

    /**
     * Getting a list of attributes for a user given by userId.
     * @param userId Id of the user
     * @param ssoId Single Sign On Id of the user that want to run this method.
     * @return List of attributes for given user
     * @throws Exception
     */
    public static List<Attribute> getList(int userId, String ssoId) throws Exception {
        AttributeWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        int groupId = AttributeWebserviceLib.getLicenseGroup(ssoId);
        if(groupId!=0) {
            Group g1 = GroupFacade.get(groupId);
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.ATTRIBUTE_GETLIST, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        return AttributeFacade.getList(userId);
    }

    /**
     * Return a list of attributes of a user (given by ssoId).
     * @param ssoId Single Sign On Id
     * @return List of attributes
     * @throws Exception
     */
    public static List<Attribute> getList1(String ssoId) throws Exception {
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        int groupId = AttributeWebserviceLib.getLicenseGroup(ssoId);
        if(groupId!=0) {
            Group g1 = GroupFacade.get(groupId);
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.ATTRIBUTE_GETLIST, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        if(SSOFacade.sessionIsActiv(ssoId)) {
            return AttributeFacade.getList(sso.getUserId());
        }
        throw new Exception("Session is not activ.");
    }
}